package com.hbsoft.com;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MyJTextField extends JTextField 
{
	private static final long serialVersionUID = -3438563085728628986L;
	private int limit = Integer.MAX_VALUE;
	private boolean numberOnly;

	public MyJTextField() 
	{
		initEventListeners();
	}

	private void initEventListeners() 
	{
		addKeyListener(new KeyAdapter() 
		{
			public void keyTyped(KeyEvent e) 
			{
				char input = e.getKeyChar();
				
				boolean ignoreInput = input == (char) KeyEvent.VK_ESCAPE
						|| input == (char) KeyEvent.VK_BACK_SPACE
						|| input == (char) KeyEvent.VK_ENTER
						|| input == (char) KeyEvent.VK_DELETE;
				if (ignoreInput) 
				{
					return;
				}
				if (getText().length() + 1 > limit) {
					Toolkit.getDefaultToolkit().beep();
					deleteInputChar(e);
					return;
				} 
				if (numberOnly) 
				{
					if (!Character.isDigit(input)) 
					{
						Toolkit.getDefaultToolkit().beep();
						deleteInputChar(e);
					} 
				}
			}

			private void deleteInputChar(KeyEvent source) {
				source.setKeyChar((char) KeyEvent.VK_CLEAR);
			}
		});
	}

	public void setMaxTextLength(int limit) 
	{
		if (limit < 0) 
		{
			return;
		}
		this.limit = limit;
	}

	public int getMaxTextLength()
	{
		return limit;
	}

	public void setNumberOnly(boolean numberOnly) 
	{
		this.numberOnly = numberOnly;
	}

	public boolean isNumberOnly() 
	{
		return this.numberOnly;
	}

	private class CoolToolTip extends JPanel 
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 6720966139547816919L;
		private JLabel label = new JLabel();
		private boolean haveShowPlace;

		private Component attachedComponent;

		public CoolToolTip(Component attachedComponent, Color fillColor,
				int borderWidth, int offset) {
			this.attachedComponent = attachedComponent;

			label.setBorder(new EmptyBorder(borderWidth, borderWidth,
					borderWidth, borderWidth));
			label.setBackground(fillColor);
			label.setOpaque(true);
			label.setFont(new Font("system", 0, 12));

			setOpaque(false);

			this.setLayout(new BorderLayout());
			add(label);

			setVisible(false);

			attachedComponent.addComponentListener(new ComponentAdapter() {
				public void componentMoved(ComponentEvent e) {
					if (isShowing()) {
						determineAndSetLocation();
					}
				}
			});
		}
		
		private void determineAndSetLocation() 
		{
			Point location = attachedComponent.getLocation();
			setBounds(location.x, location.y - getPreferredSize().height,
					getPreferredSize().width, getPreferredSize().height);
		}

		public void setText(String text) 
		{
			label.setText(text);
		}

		public void setIcon(Icon icon) 
		{
			label.setIcon(icon);
		}

		public void setIconTextGap(int iconTextGap) 
		{
			label.setIconTextGap(iconTextGap);
		}

		public void setVisible(boolean show) 
		{
			if (show) {
				determineAndSetLocation();
				findShowPlace();
			}
			super.setVisible(show);
		}

		private void findShowPlace() 
		{
			if (haveShowPlace) 
			{
				return;
			}
			
			Container parent = attachedComponent.getParent();
			JLayeredPane layeredPane;
			while (true) 
			{
				if (parent instanceof JFrame) {
					layeredPane = ((JFrame) parent).getLayeredPane();
					break;
				} else if (parent instanceof JDialog) {
					layeredPane = ((JDialog) parent).getLayeredPane();
					break;
				}
				parent = parent.getParent();
			}
			layeredPane.add(this, JLayeredPane.POPUP_LAYER);
			haveShowPlace = true;
		}
	}
}
